#include <REGX52.H>
#include "DS1302.h"
#include "LCD1602.h"
#include "key.h"
#include "Timer0.h"

unsigned int count1=0;//用来计数
unsigned char key_num;
unsigned char mode=0;
unsigned char time_set_select;//时间选择位
unsigned char space_set=0;//用来控制位闪烁

void Set_clock()//设置时间
{
		if(key_num==2)
		{
			time_set_select++;
			time_set_select %= 6;
			LCD_ShowNum(1,14,time_set_select,1);
		}
		
		else if(key_num==3)
		{
			Time[time_set_select]++;
			
			if(Time[0]>99){Time[0]=0;}//判断年的越界
			
			if(Time[1]>12){Time[1]=1;}//判断月的越界
			
			//判断日的越界
			if((Time[1]==1||Time[1]==3||Time[1]==5||Time[1]==7||Time[1]==8||Time[1]==10||Time[1]==12)&&Time[2]>31)//判断超过31天
			{Time[2]=1;}
			
			//判断是否是二月份
			if(Time[1]==2)//如果是二月
			{
				if((Time[0]%4==0&&Time[0]%100!=0)||Time[0]%400==0)//瑞年
				{
					if(Time[2]>29)
					{
						Time[2]=1;
					}
				}
				else//如果不是瑞年
				{
					if(Time[2]>28)
					{
						Time[2]=1;
					}
				}
			}
			
			if(Time[1]==4 || Time[1]==6 || Time[1]==9 || Time[1]==11)//判断30天的月份
			{
				 if(Time[2]>30)
				 {
					Time[2]=1;
				 }
			}
			
			if(Time[3]>23){Time[3]=0;}//判断时
			if(Time[4]>59){Time[4]=0;}//判断分
			if(Time[5]>59){Time[5]=0;}//判断秒
			
		}
		else if(key_num==4)
		{
			Time[time_set_select]--;
			
			if(Time[0]<0){Time[0]=99;}//判断年的越界
			
			if(Time[1]<1){Time[1]=12;}//判断月的越界
			
			//判断日的越界
			if((Time[1]==1||Time[1]==3||Time[1]==5||Time[1]==7||Time[1]==8||Time[1]==10||Time[1]==12)&&Time[2]<1)//判断超过31天
			{Time[2]=31;}
			
			//判断是否是二月份
			if(Time[1]==2)//如果是二月
			{
				if((Time[0]%4==0&&Time[0]%100!=0)||Time[0]%400==0)//瑞年
				{
					if(Time[2]<1)
					{
						Time[2]=29;
					}
					if(Time[2]>29)
					{
						Time[2]=1;
					}
				}
				else//如果不是瑞年
				{
					if(Time[2]<1)
					{
						Time[2]=28;
					}
					if(Time[2]>28)
					{
						Time[2]=1;
					}
				}
			}
			
			if(Time[1]==4 || Time[1]==6 || Time[1]==9 || Time[1]==11)//判断30天的月份
			{
				 if(Time[2]<1)
				 {
					Time[2]=30;
				 }
				 if(Time[2]>30)
				 {
					 Time[2]=1;
				 }
			}
			
			if(Time[3]<0){Time[3]=23;}//判断时
			if(Time[4]<0){Time[4]=59;}//判断分
			if(Time[5]<0){Time[5]=59;}//判断秒
		}
		Set_Time();
		if(time_set_select==0&&space_set==1)
		{
			LCD_ShowString(1,1,"  ");
		}
		else
		{
			LCD_ShowNum(1,1,Time[0],2);
		}
		
		if(time_set_select==1&&space_set==1)
		{
			LCD_ShowString(1,4,"  ");
		}
		else
		{
			LCD_ShowNum(1,4,Time[1],2);
		}	
		
		if(time_set_select==2&&space_set==1)
		{
			LCD_ShowString(1,7,"  ");
		}
		else
		{
			LCD_ShowNum(1,7,Time[2],2);
		}		
		
		if(time_set_select==3&&space_set==1)
		{
			LCD_ShowString(2,1,"  ");
		}
		else
		{
			LCD_ShowNum(2,1,Time[3],2);
		}		
		
		if(time_set_select==4&&space_set==1)
		{
			LCD_ShowString(2,4,"  ");
		}
		else
		{
			LCD_ShowNum(2,4,Time[4],2);
		}		
		
		if(time_set_select==5&&space_set==1)
		{
			LCD_ShowString(2,7,"  ");
		}
		else
		{
			LCD_ShowNum(2,7,Time[5],2);
		}
}

int main()
{
	LCD_Init();
	Timer0_Init();
	Set_Time();//设置时间
	LCD_ShowString(1,1,"  -  -  ");
	LCD_ShowString(2,1,"  :  :  ");
	while(1)
	{
		key_num=KeySelect();
		if(key_num==1)
		{
			mode=!mode;
		}
		if(mode==0)
		{
			Read_Time();//读取时间
		}
		else if(mode==1)
		{
			Set_clock();//设置时间
		}
	}
	return 0;
}

void Timer0_Routine() interrupt 1
{
	count1++;
	TH0=0xfc;//1111 1100
	TL0=0x18;//0001 1000
	if(count1==800)
	{
		count1=0;
		space_set=!space_set;
	}
}